<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\LogAccess;
use App\Models\Order;
use Encore\Admin\Controllers\Dashboard;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\Echarts\Echarts;
use Illuminate\Support\Facades\DB;

class HomeController extends Controller
{
    public function index(Content $content)
    {
        // bindData
        $head = [
            'time' => trans('admin.hour'),
            'login_num' => trans('admin.login_amount'),
        ];

        $charts = (new Echarts(trans('admin.current_access_amount')))
            ->setSeriesType('line')
            ->setData($this->get24HoursAccessData())
            ->bindLegend($head);

        return $content
            ->header(trans('admin.data_info'))
            ->description('description')
            ->row(function (Row $row) use ($charts) {
                $row->column(12, new Box(trans('admin.basic_info'), $this->firstHtml()));
                $row->column(12, new Box(trans('admin.active_users'), $charts));
            });
    }

    private function firstHtml()
    {
        return '
        <div class="row">
            <div class="col-lg-2 col-xs-6">
              <!-- small box -->
              <div class="small-box bg-aqua">
                <div class="inner">
                  <h3>' . $this->getTodayAccessData() . '</h3>
    
                  <p>' . trans('admin.today_access_amount') . '</p>
                </div>
                <div class="icon">
                  <i class="fa fa-heartbeat"></i>
                </div>
                
              </div>
            </div>
            <!-- ./col -->
            <div class="col-lg-2 col-xs-6">
              <!-- small box -->
              <div class="small-box bg-green">
                <div class="inner">
                  <h3>' . $this->getTotalUsersData() . '</h3>
    
                  <p>' . trans('admin.total_users_amount') . '</p >
                </div >
                <div class="icon" >
                  <i class="fa fa-users" ></i >
                </div >
                
              </div >
            </div >
            <!-- ./col-->
            <div class="col-lg-2 col-xs-6" >
              <!--small box-->
              <div class="small-box bg-yellow" >
                <div class="inner" >
                  <h3 > ' . $this->getTodayDealAmountData() . '</h3 >
    
                  <p > ' . trans('admin.today_deal_amount') . '</p >
                </div >
                <div class="icon" >
                  <i class="fa fa-yen" ></i >
                </div >
                
              </div >
            </div >
            <!-- ./col-->
            <div class="col-lg-2 col-xs-6" >
              <!--small box-->
              <div class="small-box bg-green" >
                <div class="inner" >
                  <h3 > ' . $this->getMonthDealAmountData() . '</h3 >
    
                  <p > ' . trans('admin.current_month_deal_amount') . '</p >
                </div >
                <div class="icon" >
                  <i class="fa fa-dollar" ></i >
                </div >
                
              </div >
            </div >
            <!-- ./col-->
            <div class="col-lg-2 col-xs-6" >
              <!--small box-->
              <div class="small-box bg-red" >
                <div class="inner" >
                  <h3 > ' . $this->getTodayOrdersData() . '</h3 >
    
                  <p > ' . trans('admin.today_orders_amount') . '</p >
                </div >
                <div class="icon" >
                  <i class="fa fa-area-chart" ></i >
                </div >
                
              </div >
            </div >
            <!-- ./col-->
            <div class="col-lg-2 col-xs-6" >
              <!--small box-->
                <div class="small-box bg-aqua" >
                    <div class="inner" >
                      <h3 >' . $this->getMonthOrdersData() . '</h3 >
        
                      <p > ' . trans('admin.current_month_orders_amount') . '</p >
                    </div >
                    <div class="icon" >
                      <i class="fa fa-shopping-cart" ></i >
                    </div >
                    
                </div >
            </div >
        </div >';
    }

    private function get24HoursAccessData()
    {
        $query = 'SELECT count(*) as login_num,HOUR(created_at) as time FROM `sp_log_access` WHERE TO_DAYS(created_at) = TO_DAYS(NOW()) GROUP BY HOUR(created_at)';

        $db_data = DB::select($query);

        //格式化为2维数组
        $db_data = array_map(function ($data) use ($db_data) {
            return (array)$data;
        }, $db_data);

        $data = [];

        for ($i = 0; $i < 24; $i++) {
            $k = $i;

            $data[$k]['login_num'] = 0;
            $data[$k]['time'] = $i;
        }

        $access_data = array_map(function ($d) use ($db_data) {
            foreach ($db_data as $item) {
                if ($item['time'] == $d['time']) {
                    $d['login_num'] = $item['login_num'];
                }
            }

            $d['time'] = $d['time'] . ':00';

            return $d;
        }, $data);

        return $access_data;
    }

    private function getTodayAccessData()
    {
        $data = DB::table('log_access')->whereDate('created_at', date('Y-m-d'))->distinct()->pluck('ip')->count();

        return $data;
    }

    private function getTotalUsersData()
    {
        $data = DB::table('users')->count('id');

        return $data;
    }

    private function getTodayDealAmountData()
    {
        $data = DB::table('orders')->where('status', Order::STATUS_FINISH)->where('refund_status', '!=', Order::REFUND_STATUS_SUCCESS)->whereDay('created_at', date('d'))->sum('total_amount');

        return $data;
    }

    private function getMonthDealAmountData()
    {
        $data = DB::table('orders')->where('status', Order::STATUS_FINISH)->where('refund_status', '!=', Order::REFUND_STATUS_SUCCESS)->whereMonth('created_at', date('m'))->sum('total_amount');

        return $data;
    }

    private function getTodayOrdersData()
    {
        $data = DB::table('orders')->whereDay('created_at', date('d'))->count('id');

        return $data;
    }

    private function getMonthOrdersData()
    {
        $data = DB::table('orders')->whereMonth('created_at', date('m'))->count('id');

        return $data;
    }
}
